package com.cby.android.sample.activities

import android.text.TextUtils
import android.widget.Button
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cby.android.library.base.BaseStateActivity
import com.cby.android.library.base.callback.EmptyCallback
import com.cby.android.library.base.callback.ErrorCallback
import com.cby.android.library.base.callback.LottieLoadingCallback
import com.cby.android.library.base.callback.TimeoutCallback
import com.cby.android.library.ext.observe
import com.cby.android.library.utils.KeyboardUtils
import com.cby.android.library.widget.edittext.MClearEditText
import com.cby.android.library.widget.snackbar.MySnackbar
import com.cby.android.sample.R
import com.cby.android.sample.adapter.RollRubbishAdapter
import com.cby.android.sample.bean.roll.RollAimBean
import com.cby.android.sample.intent.RollMoreIntent
import com.cby.android.sample.model.RollMoreViewModel
import com.cby.android.sample.state.RollMoreState
import com.kingja.loadsir.core.LoadSir
import kotlinx.coroutines.launch
import kotlin.math.abs

/**
 * @description: RollRubbishActivity
 *
 * @author ChenBaoyang
 * @date 4/25/21 10:15
 */
class RollRubbishActivity : BaseStateActivity<RollMoreViewModel, RollMoreState, RollMoreIntent>(){

    private val queryBtn: Button by lazy { findViewById(R.id.btn_query) }
    private val rubbishEdit: MClearEditText by lazy { findViewById(R.id.edit_text_rubbish) }
    private val recyclerView: RecyclerView by lazy { findViewById(R.id.recyclerView) }

    private lateinit var adapter: RollRubbishAdapter

    override fun bindLiveData() {
        observe(viewModel!!.state) {
            if(it.dataRubbish != null) {
                val datas = mutableListOf<RollAimBean>()
                datas.add(it.dataRubbish!!.aim)
                datas.addAll(it.dataRubbish!!.recommendList)
                adapter.setNewInstance(datas)
            }else{
                pageEmpty()
            }

        }
    }

    override fun onViewInit() {
        super.onViewInit()
        title = "垃圾分类"

        adapter = RollRubbishAdapter()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addOnScrollListener(object :
            RecyclerView.OnScrollListener() {
            override fun onScrolled(
                recyclerView: RecyclerView,
                dx: Int,
                dy: Int
            ) {
                if (abs(dy) > 20) {
                    KeyboardUtils.hideInputKeyboard(this@RollRubbishActivity)
                }
                super.onScrolled(recyclerView, dx, dy)
            }
        })

        queryBtn.setOnClickListener {
            //获取关键字
            val keyWord = rubbishEdit.text.toString()
            if (TextUtils.isEmpty(keyWord)) {
                MySnackbar.makeSnackBarBlack(toolbar, "查询内容不能为空")
                return@setOnClickListener
            }
            KeyboardUtils.hideInputKeyboard(this@RollRubbishActivity)

            lifecycleScope.launch {
                viewModel!!.intents.send(RollMoreIntent.LoadRollRubbishIntent(keyWord))
            }
        }

    }

    override fun initLoadSir() {
        val loadSir = LoadSir.Builder()
            .addCallback(LottieLoadingCallback())
            .addCallback(EmptyCallback())
            .addCallback(ErrorCallback())
            .addCallback(TimeoutCallback())
            .setDefaultCallback(EmptyCallback::class.java)
            .build()
        loadService = loadSir.register(getTargetView()){
            reload()
        }
    }

    override fun reload() {

    }

    override fun getLayoutId() = R.layout.roll_activity_rubbish

    override fun getTargetView() = recyclerView
}